package com.vibration.baseinfo.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vibration.common.utils.DateUtils;
import com.vibration.utils.CardFlowAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vibration.baseinfo.mapper.CzCardMapper;
import com.vibration.baseinfo.domain.CzCard;
import com.vibration.baseinfo.service.ICzCardService;

/**
 * 物联网卡Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2023-01-31
 */
@Service
public class CzCardServiceImpl implements ICzCardService 
{
    @Autowired
    private CzCardMapper czCardMapper;

    /**
     * 查询物联网卡
     * 
     * @param id 物联网卡主键
     * @return 物联网卡
     */
    @Override
    public CzCard selectCzCardById(Long id)
    {
        return czCardMapper.selectCzCardById(id);
    }

    /**
     * 查询物联网卡列表
     * 
     * @param czCard 物联网卡
     * @return 物联网卡
     */
    @Override
    public List<CzCard> selectCzCardList(CzCard czCard)
    {
        return czCardMapper.selectCzCardList(czCard);
    }

    /**
     * 新增物联网卡
     * 
     * @param czCard 物联网卡
     * @return 结果
     */
    @Override
    public int insertCzCard(CzCard czCard)
    {
        return czCardMapper.insertCzCard(czCard);
    }

    /**
     * 修改物联网卡
     * 
     * @param czCard 物联网卡
     * @return 结果
     */
    @Override
    public int updateCzCard(CzCard czCard)
    {
        czCard.setUpdateTime(DateUtils.getNowDate());
        return czCardMapper.updateCzCard(czCard);
    }

    /**
     * 批量删除物联网卡
     * 
     * @param ids 需要删除的物联网卡主键
     * @return 结果
     */
    @Override
    public int deleteCzCardByIds(Long[] ids)
    {
        return czCardMapper.deleteCzCardByIds(ids);
    }

    /**
     * 删除物联网卡信息
     * 
     * @param id 物联网卡主键
     * @return 结果
     */
    @Override
    public int deleteCzCardById(Long id)
    {
        return czCardMapper.deleteCzCardById(id);
    }

    @Override
    public void updateCardFlow() {
        List<CzCard> cardList = czCardMapper.selectCzCardList(new CzCard());
        Map<String,CzCard> cardMap= new HashMap<>();
        for (CzCard czCard : cardList) {
            cardMap.put(czCard.getIccid(),czCard);
        }
        //接口查询流量
        String loginResult = CardFlowAPI.login();
        JSONObject loginObj = JSONObject.parseObject(loginResult);
        if(200 == loginObj.getInteger("status")){
            JSONObject dataObj=loginObj.getJSONObject("data");
            String token = dataObj.getString("token");

            JSONObject cardObj = JSONObject.parseObject(CardFlowAPI.haveCardList(token));
            System.err.println(cardObj);
            if(200 == cardObj.getInteger("status")){
                JSONObject cardDataObj=cardObj.getJSONObject("data");
                JSONArray dataList = cardDataObj.getJSONArray("list");
                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject object=dataList.getJSONObject(i);
                    String iccid = object.getString("iccid");
                    if(cardMap.containsKey(iccid)){
                        CzCard card = cardMap.get(iccid);
                        card.setOperatorName(object.getString("operator_name"));
                        card.setFlowRate(object.getString("flow_rate"));
                        String useFlow = object.getString("use_flow");
                        card.setUseFlow(Double.parseDouble(useFlow.substring(0,useFlow.length() - 2)));
                        if("MB".equals(useFlow.substring(useFlow.length() - 2))){
                            card.setUseUnit(0);
                        }else {
                            card.setUseUnit(1);
                        }
                        String surplusFlow = object.getString("surplus_flow");
                        card.setSurplusFlow(Double.parseDouble(surplusFlow.substring(0,surplusFlow.length() - 2)));
                        if("MB".equals(surplusFlow.substring(surplusFlow.length() - 2))){
                            card.setSurplusUnit(0);
                        }else {
                            card.setSurplusUnit(1);
                        }
                        card.setActivationTime(object.getDate("activation_time"));
                        card.setEndTime(object.getDate("end_time"));
                        if("停机".equals(object.getString("content"))){
                            card.setCardStatus(0);
                        }else if("正常".equals(object.getString("content"))){
                            card.setCardStatus(1);
                        }else if("测试".equals(object.getString("content"))){
                            card.setCardStatus(2);
                        }else if("沉默".equals(object.getString("content"))){
                            card.setCardStatus(3);
                        }else {
                            card.setCardStatus(4);
                        }
                        card.setUpdateTime(object.getDate("time"));
                        //判断预警状态
                        card.setWarnStatus(0);
                        if( card.getEndTime() !=null && card.getEndTime().getTime()-new Date().getTime() < 0 ){ //过期
                            card.setWarnStatus(3);
                        }else{
                            if (card.getEndTime() !=null && card.getEndTime().getTime()-new Date().getTime() < 30*24*60*60*1000){
                                card.setWarnStatus(2);
                            }else {
                                if(card.getSurplusUnit() == 0 && card.getUseUnit() == 0 && (card.getUseFlow()+card.getSurplusFlow())<11){

                                }else {
                                    if(card.getSurplusUnit() == 0 && card.getSurplusFlow()<100){
                                        card.setWarnStatus(1);
                                    }
                                }

                            }

                        }

                        updateCzCard(card);
                    }
                }
            }
        }
    }

    @Override
    public Integer selectCzCardWarnCount() {
        return czCardMapper.selectCzCardWarnCount();
    }
}
